package com.wrial.middle;
/*
 * @Author  Wrial
 * @Date Created in 21:58 2020/7/26
 * @Description 找到给定的二维数组中最大的岛屿面积
 */

public class LeetCode_695_岛屿问题 {

    public int maxAreaOfIsland(int[][] grid) {
        //1.非法值判断
        if (grid.length == 0 || grid == null) return 0;
        int res = 0;
        //2.遍历寻找
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                // 挑选出最大的
                res = Math.max(res, area(grid, i, j));
            }
        }

        return res;

    }

    private int area(int[][] grid, int i, int j) {
        if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] != 1) return 0;
        grid[i][j] = 2;//访问过后都改为2
        return 1 + area(grid, i - 1, j) + area(grid, i, j + 1)
                + area(grid, i + 1, j) + area(grid, i, j - 1);
    }
}
